#include "Stack.h"

void StackInit(Stack* SK)
{
	assert(SK);

	SK->data = NULL;
	SK->capacity = 0;
	SK->size = 0;
}

void StackDestroy(Stack* SK)
{
	assert(SK);

	free(SK->data);
	SK->capacity = 0;
	SK->size = 0;
	SK->data = NULL;
}

void StackPush(Stack* SK,StackData x)
{
	assert(SK);

	if (SK->capacity == SK->size)
	{
		SK->capacity = (SK->capacity == 0 ? 5 : SK->capacity * 2);
		StackData* tmp = (StackData*)realloc(SK->data,sizeof(StackData) *SK->capacity);
		if (tmp == NULL)
		{
			perror("realloc ");
			exit(-1);
		}
		SK->data = tmp;
	}
	SK->data[SK->size] = x;
	SK->size++;
}

void StackPop(Stack* SK)
{
	assert(SK);
	assert(SK->size > 0);
	SK->size--;
}

StackData StackTop(Stack* SK)
{
	assert(SK);

	assert(SK->size > 0);
	return SK->data[SK->size-1];
}

bool StackEmpty(Stack* SK)
{
	assert(SK);

	return SK->size == 0;
}

int StackSize(Stack* SK)
{
	assert(SK);

	return SK->size;
}